package org.jeecg.modules.manager.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import java.util.Date;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.manager.entity.Repository;
import org.jeecg.modules.manager.entity.Stock;
import org.jeecg.modules.manager.entity.StockQueryVo;
import org.jeecg.modules.manager.mapper.RepositoryMapper;
import org.jeecg.modules.manager.mapper.StockMapper;
import org.jeecg.modules.manager.service.IPurchaseOrderService;
import org.jeecg.modules.manager.service.ISaleOrderService;
import org.jeecg.modules.manager.service.IStockService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 库存表
 * @Author: jeecg-boot
 * @Date:   2021-11-28
 * @Version: V1.0
 */
@Slf4j
@Api(tags="库存表")
@RestController
@RequestMapping("/manager.controller/stock")
public class StockController extends JeecgController<Stock, IStockService> {
	@Autowired
	private IStockService stockService;

	@Autowired
	private StockMapper stockMapper;

	@Autowired
	private RepositoryMapper repositoryMapper;

	@Autowired
	private ISaleOrderService saleOrderService;

	@Autowired
	private IPurchaseOrderService purchaseOrderService;
	/**
	 * 分页列表查询
	 *
	 * @param
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "库存表-分页列表查询")
	@ApiOperation(value="库存表-分页列表查询", notes="库存表-分页列表查询 可查询字段 goodsname，repoName")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(StockQueryVo vo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		return Result.OK(stockService.getPage(vo));
	}

	 @GetMapping(value = "/citys")
	 public Result<?> getCitys() {
		 List<String> collect = repositoryMapper.selectList(new QueryWrapper<>()).stream().map(Repository::getRepoAddress).distinct().collect(Collectors.toList());
		 return Result.OK(collect);
	 }

	 /**
	  * 入库 即添加库存
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "采购订单表-入库 即添加库存")
	 @ApiOperation(value="采购订单表-入库 即添加库存 传那个订单组的id即可", notes="采购订单表-入库 即添加库存 传那个订单组的id即可")
	 @GetMapping(value = "/include")
	 public Result<?> include(@RequestParam(name="id",required=true) String id) {
		 return purchaseOrderService.includeStock(id,currentUser()) == 0 ? Result.OK("ok"):Result.error("查询失败！");
	 }

	 /**
	  * 入库 即添加库存
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "销售订单表-出库 即减少库存")
	 @ApiOperation(value="销售订单表-出库 即减少库存 传那个订单组的id即可", notes="销售订单表-出库 即减少库存 传那个订单组的id即可")
	 @GetMapping(value = "/out")
	 public Result<?> out(@RequestParam(name="id",required=true) String id) {
		 return saleOrderService.outStock(id,currentUser()) == 0 ? Result.OK("ok"):Result.error("查询失败！");
	 }

  /**
   * 导出excel
   *
   * @param request
   * @param stock
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, Stock stock) {
      return super.exportXls(request, stock, Stock.class, "库存表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Stock.class);
  }

}
